package com.webmining.sherlock.dao;


import com.webmining.sherlock.dto.Role;
import com.webmining.sherlock.util.DataEncryptor;
import com.webmining.sherlock.util.UserContainer;

public class UserDaoForMap implements UserDao{

    private static UserDao userDao = null;

    private UserContainer userContainer = UserContainer.getInstance();

    private UserDaoForMap(){

    }

    public static synchronized UserDao getInstance(){
        if(userDao == null){
            userDao = new UserDaoForMap();
        }
        return userDao;
    }

    @Override
    public boolean userExists(String login) {
        return userContainer.userExists(login);
    }

    @Override
    public boolean isValidPassword(String user, String password) {
        if (userExists(user)) {
            String encryptedPassword = DataEncryptor.encrypt(password);
            return encryptedPassword.equals(userContainer.getUserPassword(user));
        }
        return false;
    }

    @Override
    public void addUser(String login, String password){
        if (login == null || password == null)
            throw new IllegalArgumentException("Login an Password must be not null.");

        userContainer.addUser(login, password);
    }

    @Override
    public boolean isAdmin(String login) {
        return userContainer.getUserRole(login) == Role.ADMIN;
    }
}
